/*
 *  AMPRequestTask.cpp
 *  AMPlugin
 *
 *  Created by Toan on 12/13/12.
 *  Copyright 2012 __MyCompanyName__. All rights reserved.
 *
 */


#include "VCPlugInHeaders.h"

// interface includes
#include "IIdleTaskMgr.h"

// General includes:
#include "CIdleTask.h"

// Project includes
#include "AMPID.h"
#include <iostream>
#include "JSONHelper.h"
#include "HTTPRequestHelper.h"
#include "ISelectionUtils.h"
#include "AMPLogDataRecord.h"
#include "Notifier.h"
#include "AMGlobal.h"
#include "AMPCAlert.h"
	
const static int kAMPRequestInterval = 5*1000; // 5 seconds, as we specify milliseconds between checks	
// Refresh  in case we add new images


/** Implements IIdleTask, to refresh the view onto the file system.
 Just lets us keep up to date when people add new assets.
 This implementation isn't too respectful; it just clears the tree and calls ChangeRoot.
 
 
 @ingroup paneltreeview
 */
class AMPRequestTask : public CIdleTask
	{
	public:
		
		/**	Constructor
		 @param boss boss object on which this interface is aggregated
		 */
		AMPRequestTask(IPMUnknown* boss);
		
		/**	Destructor
		 */
		virtual ~AMPRequestTask() {}
		
		
		/**	Called by the idle task manager in the app core when the task is running
		 @param appFlags [IN] specifies set of flags which task can check to see if it should do something
		 this time round
		 @param timeCheck [IN] specifies how many milliseconds before the next active task becomes overdue.
		 @return uint32 giving the interval (msec) that should elapse before calling this back
		 */
		virtual uint32 RunTask(uint32 appFlags, IdleTimer* timeCheck);
		
		/**	Get name of task
		 @return const char* name of task
		 */
		virtual const char* TaskName();

	protected:
	};

/* CREATE_PMINTERFACE
 Binds the C++ implementation class onto its ImplementationID 
 making the C++ code callable by the application.
 */
CREATE_PMINTERFACE(AMPRequestTask, kAMPRequestTaskImpl)


/* Constructor
 */
AMPRequestTask::AMPRequestTask(IPMUnknown *boss)
:CIdleTask(boss)
{
}


/* RunTask
 */
uint32 AMPRequestTask::RunTask(
								 uint32 appFlags, IdleTimer* timeCheck)
{
	if( appFlags & 
	   ( 
	//	IIdleTaskMgr::kMouseTracking 
	//	IIdleTaskMgr::kUserActive 
		IIdleTaskMgr::kInBackground 
		| IIdleTaskMgr::kMenuUp
		| IIdleTaskMgr::kApplicationMinimized
	//	| IIdleTaskMgr::kMainThreadTaskQueued
	   ))
	{
		return kOnFlagChange;
	}
	
	//cout << "Update status icon stories" << endl;
	if((g_IsLoggedIn == TRUE)&&(g_Package_id != "")) {
		HTTPRequestHelper::UpdateStoryStatus();
	}
	// Has FS changed?
	return kAMPRequestInterval;
}

const char* AMPRequestTask::TaskName()
{
	return "requestHTTPStatus";
}